#include "U2HardwareIndexBuffer.h"

#include "U2HardwareBufferManager.h"
#include "U2DefaultHardwareBufferManager.h"


U2EG_NAMESPACE_USING


//-----------------------------------------------------------------------------
U2HardwareIndexBuffer::U2HardwareIndexBuffer(U2HardwareBufferManagerBase* mgr, IndexType idxType, 
    size_t numIndexes, U2HardwareBuffer::Usage usage, 
    bool useSystemMemory, bool useShadowBuffer) 
    : U2HardwareBuffer(usage, useSystemMemory, useShadowBuffer)
	, mMgr(mgr)
	, mIndexType(idxType)
	, mNumIndexes(numIndexes)
{
    // Calculate the size of the indexes
    switch (mIndexType)
    {
    case IT_16BIT:
        mIndexSize = sizeof(unsigned short);
        break;
    case IT_32BIT:
        mIndexSize = sizeof(unsigned int);
        break;
    }
    mSizeInBytes = mIndexSize * mNumIndexes;

    // Create a shadow buffer if required
    if (mUseShadowBuffer)
    {
        mpShadowBuffer = U2_NEW U2DefaultHardwareIndexBuffer(mIndexType, 
            mNumIndexes, U2HardwareBuffer::HBU_DYNAMIC);
    }


}
//-----------------------------------------------------------------------------
U2HardwareIndexBuffer::~U2HardwareIndexBuffer()
{
	if (mMgr)
	{
		mMgr->_notifyIndexBufferDestroyed(this);
	}

    if (mpShadowBuffer)
    {
        U2_DELETE mpShadowBuffer;
    }
}





//-----------------------------------------------------------------------------
U2HardwareIndexBufferSharedPtr::U2HardwareIndexBufferSharedPtr(U2HardwareIndexBuffer* buf)
    : U2SharedPtr<U2HardwareIndexBuffer>(buf)
{

}

